package org.duiduo.controller;

import lombok.extern.slf4j.Slf4j;
import org.duiduo.annotation.AuthLogin;
import org.duiduo.annotation.LogPush;
import org.duiduo.constant.ResponseConstants;
import org.duiduo.enums.UserRequestEnum;
import org.duiduo.factory.TbUserServiceFactory;
import org.duiduo.pojo.TbUser;
import org.duiduo.result.BaseResult;
import org.duiduo.result.ResultData;
import org.duiduo.service.TbUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Controller层
 *
 * @author 夜泷noga
 */
// 登录拦截 默认 true 需要登录
@AuthLogin
@Slf4j
@Controller
@RequestMapping("/user")
public class TbUserController {

    @Resource
    private TbUserServiceFactory userServiceFactory;

    // TODO 可以切换工厂(使用缓存/不使用缓存) - 查询列表 如果使用缓存 则不能使用用户名查询条件 - 目前是不用缓存
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(defaultValue = "", required = false) String username) {
        ModelAndView mView = new ModelAndView();
        TbUserService userService = userServiceFactory.getUserServiceImpl(UserRequestEnum.USER_REQUEST_TYPE_NOMAL.getUserRequestType());
        if (userService == null) {
            log.error("userService为空,找不到用户请求类型");
            return BaseResult.errorResult("userService为空,找不到用户请求类型");
        }
        List<TbUser> userList = null;
        try {
            userList = userService.getList(username);
            mView.addObject("list", userList);
            mView.addObject("username", username);
            mView.setViewName("list");
            return mView;
        } catch (Exception e) {
            log.error("异常:", e);
            return BaseResult.errorResult(e.getMessage());
        }
    }

    // TODO 可以切换工厂(使用缓存/不使用缓存) - 删除 - 目前是不用缓存
    @GetMapping("/delete")
    public ModelAndView delete(@RequestParam Integer id) {
        TbUserService userService = userServiceFactory.getUserServiceImpl(UserRequestEnum.USER_REQUEST_TYPE_NOMAL.getUserRequestType());
        if (userService == null) {
            log.error("userService为空,找不到用户请求类型");
            return BaseResult.errorResult("userService为空,找不到用户请求类型");
        }
        userService.delete(id);
        return new ModelAndView("redirect:list");
    }

    // TODO 可以切换工厂(使用缓存/不使用缓存) - 添加修改 - 目前是不用缓存
    @LogPush(type = "EDITADD")
    @PostMapping("/addOrEdit")
    public ModelAndView addOrEdit(TbUser user) {
        TbUserService userService = userServiceFactory.getUserServiceImpl(UserRequestEnum.USER_REQUEST_TYPE_NOMAL.getUserRequestType());
        if (userService == null) {
            log.error("userService为空,找不到用户请求类型");
            return BaseResult.errorResult("userService为空,找不到用户请求类型");
        }
        ResultData data = userService.addOrEdit(user);
        // 如果不是响应成功 则跳转到失败页面 展示失败信息
        if (!ResponseConstants.Resp.RESP_00.getCode().equals(data.getCode())) {
            return BaseResult.errorResult(data.getMessage());
        }
        return new ModelAndView("redirect:list");
    }

    @GetMapping("/toEdit")
    public ModelAndView toEdit(@RequestParam(defaultValue = "0") Integer id) {
        TbUserService userService = userServiceFactory.getUserServiceImpl(UserRequestEnum.USER_REQUEST_TYPE_NOMAL.getUserRequestType());
        if (userService == null) {
            log.error("userService为空,找不到用户请求类型");
            return BaseResult.errorResult("userService为空,找不到用户请求类型");
        }
        ModelAndView mView = new ModelAndView();
        String content = "";
        if (id > 0) {
            // 编辑
            content = "编辑用户";
            mView.addObject("user", userService.findById(id));
        } else {
            // 添加
            content = "添加用户";
            TbUser user = TbUser.builder()
                    .username("")
                    .password("")
                    .build();
            mView.addObject("user", user);
        }
        mView.addObject("content", content);
        mView.setViewName("addOrEdit");
        return mView;
    }
}
